local joy__fuhan = fk.CreateSkill {

  name = "joy__fuhan",

  tags = { Skill.Limited, },

}



joy__fuhan:addEffect(fk.TurnStart, {
  name = "joy__fuhan",
  events = {fk.TurnStart},
  frequency = Skill.Limited,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(joy__fuhan.name) and player:getMark("@meiying") > 0 and
      player:usedSkillTimes(joy__fuhan.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, joy__fuhan.name, nil, "#joy__fuhan-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getMark("@meiying")
    room:setPlayerMark(player, "@meiying", 0)
    player:drawCards(n, joy__fuhan.name)
    if player.dead then return end
    local generals = {}
    local gens = {"joy__zhaoxiang"}
    for _, p in ipairs(room.alive_players) do
      table.insertIfNeed(gens,p.general)
      if p.deputyGeneral ~= "" then
        table.insertIfNeed(gens,p.deputyGeneral)
      end
    end
    for _, general in pairs(Fk.generals) do
      if general.package.extensionName == "joy" and general.kingdom == "shu" and not table.contains(gens,general.name) then
        table.insert(generals, general.name)
      end
    end
    if #generals > 0 then 
    generals = table.random(generals, math.max(4, #room.alive_players))
    local skills = {}
    local choices = {}
    for _, general_name in ipairs(generals) do
      local general = Fk.generals[general_name]
      local g_skills = {}
      for _, skill in ipairs(general.skills) do
        if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill) and
        (#skill.attachedKingdom == 0 or (table.contains(skill.attachedKingdom, "shu") and player.kingdom == "shu")) then
          table.insertIfNeed(g_skills, skill.name)
        end
      end
      for _, s_name in ipairs(general.other_skills) do
        local skill = Fk.skills[s_name]
        if not (table.contains({Skill.Limited, Skill.Wake, Skill.Quest}, skill.frequency) or skill.lordSkill) and
        (#skill.attachedKingdom == 0 or (table.contains(skill.attachedKingdom, "shu") and player.kingdom == "shu")) then
          table.insertIfNeed(g_skills, skill.name)
        end
      end
      table.insertIfNeed(skills, g_skills)
      if #choices == 0 and #g_skills > 0 then
        choices = {g_skills[1]}
      end
    end
    if #choices > 0 then
      local result = player.room:askForCustomDialog(player, joy__fuhan.name,
      "packages/tenyear/qml/ChooseGeneralSkillsBox.qml", {
        generals, skills, 1, 2, "#joy__fuhan-choice", false
      })
      if result ~= "" then
        choices = json.decode(result)
      end
      room:handleAddLoseSkills(player, table.concat(choices, "|"), nil)
    end
    end
    if not player.dead and player:isWounded() and
    table.every(room.alive_players, function(p) return p.hp >= player.hp end) then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = joy__fuhan.name
      })
    end
  end,
})

return joy__fuhan